package com.aosgame;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;

public class Entity {
	
	public static final int 	DRT_LEFT 		= 0;
	public static final int 	DRT_LEFT_TOP 	= 1;
	public static final int 	DRT_TOP 		= 2;
	public static final int 	DRT_RIGHT_TOP 	= 3;
	public static final int 	DRT_RIGHT 		= 4;
	public static final int 	DRT_RIGHT_BOTTOM = 5;
	public static final int 	DRT_BOTTOM 		= 6;
	public static final int 	DRT_LEFT_BOTTOM = 7;
	
	public static final byte 	STATE_IDLE 		= 0x00;
	public static final byte 	STATE_WALK 		= 0x01;
	public static final byte 	STATE_ATTACK 	= 0x02;
	public static final byte 	STATE_HIT 		= 0x04;
	public static final byte	STATE_DEAD		= 0x08;

	public static final int 	ANI_IDLE 		= 0;
	public static final int 	ANI_WALK 		= 1;
	public static final int 	ANI_ATTACK		= 2;
	public static final int 	ANI_DEAD		= 3;
	public static final int 	ANI_END			= 4;

	public Bitmap		image;
	public Point		position;
	public Rect			collisionArea;
	public Rect			maxHpRect;
	public Rect			hpRect;
		
	public float		remainX;
	public float		remainY;
	public byte			state;

	public boolean		alive;
	public int			maxHp;
	public int			hp;
	public int			power;
	public int			armor;
	public float		speed;	
	public float		angle;	

	public int			frame;
	public int			frameWidth;
	public int			frameHeight;
	public long			lastUpdateTime;
	public int			drt;
	public Animation	currentAnimation;
	public Animation[]	animations;
	
	public boolean compareState(byte state) {
		if((this.state & state) == state) {
			return true;
		}
		return false;
	}
	
	public void addState(byte state) {
		this.state |= state;
	}
	
	public void removeState(byte state) {
		this.state &= ~state;
	}
	
	public void move(int x, int y) {
		position.x += x;
		position.y += y;
		collisionArea.offset(x, y);
	}
	
	public void setPosition(int x, int y) {
		position.x = x;
		position.y = y;
		collisionArea.set(x - collisionArea.width() / 3, y - collisionArea.height() / 3, 
				x + collisionArea.width() / 3, y + collisionArea.height() / 3);
	}
	
	public void processDrt() {
		
		if(angle <  Math.toRadians(22.5)) {
			drt = DRT_RIGHT;
		} else if(angle < Math.toRadians(67.5)) {
			drt = DRT_RIGHT_TOP;
		} else if(angle < Math.toRadians(112.5)) {
			drt = DRT_TOP;
		} else if(angle < Math.toRadians(157.5)) {
			drt = DRT_LEFT_TOP;
		} else if(angle < Math.toRadians(202.5)) {
			drt = DRT_LEFT;
		} else if(angle < Math.toRadians(247.5)) {
			drt = DRT_LEFT_BOTTOM;
		} else if(angle < Math.toRadians(292.5)) {
			drt = DRT_BOTTOM;
		} else if(angle < Math.toRadians(337.5)) {
			drt = DRT_RIGHT_BOTTOM;
		} else {
			drt = DRT_RIGHT;
		}
	}
	
	public float getAngle(int srcX, int srcY, int destX, int destY) {
		float		x, y;
		float		angle;

		x = destX - srcX;
		y = destY - srcY;

		if(x == 0.0f) x = 0.000001f;
		angle = (float)Math.atan(-y / x);	
		if(x < 0) angle += Math.PI;
		if(destX >= srcX && destY >= srcY) angle += 2 * Math.PI;

		return angle;		
	}
	
	public int getDistance(int srcX, int srcY, int destX, int destY) {
		return (int)Math.sqrt((srcX - destX) * (srcX - destX) + (srcY - destY) * (srcY - destY));
	}
	
	public void damaged(int power) {
		int damage;
		if(power < armor) {
			damage = 1;
		} else {
			damage = power - armor;
		}
		
		if(hp - damage < 0) {
			hp = 0;
			addState(STATE_DEAD);
			setAnimation(ANI_DEAD);
		} else {
			hp -= damage;
		}
	}
	
	public void animation() { 
		long currentTime = System.currentTimeMillis();
		
		if(currentTime - lastUpdateTime > currentAnimation.frameInterval) {
			frame++;
			lastUpdateTime = currentTime;
			
			if(frame >= currentAnimation.frameCount) {
				if(currentAnimation.loop) {
					frame = 0;
				} else {
					frame = currentAnimation.frameCount - 1;
					currentAnimation.play = false;
				}
			}
		}
	}
	
	public void setAnimation(int index) {
		if(currentAnimation != animations[index]) {
			frame = 0;
		}
		currentAnimation.play = false;
		
		currentAnimation = animations[index];
		currentAnimation.play = true; 
	}
}
